
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;
using Syrinx.Gui;

namespace Contacts.Gui
{
	
	public class ContactManagementBP : BaseBusinessProcess
	{
		
		public ContactManagementBP(ActiveBizProc procState) : base(procState) { }

		protected override object[] getBizObjListRuleParameters(object parentId, IBizObj filterSort)
		{
			Swaf.BGuid.Guid companyId = null;
			IBizObj filterSortCriteria = null;

			return new object[] { Swaf.DetailLevel.idAndTitle,companyId,filterSortCriteria };
		}

		protected override bool shouldCallGetBizObjDetailsRule(object[] ruleParams)
		{
			return ruleParams != null && ruleParams.Length == 1 && ruleParams[0] != null;
		}

		protected override object[] getUpdateBizRuleParameters(IBizObj bizObj)
		{
			object id = m_procState.get("id");
			if (id != null && bizObj != null && bizObj.get("id") == null)
				bizObj.put("id", id);
			return base.getUpdateBizRuleParameters(bizObj);
		}
		
		protected override void setupRuleNames()
		{
			m_updateBizObjRule = "updateContact";
			m_getBizObjListRule = "getContacts";
			m_getBizObjDetailsRule = "getContactDetail";
		}

		protected override Privileges CurrentEditPrivilege
		{
			get 
			{
				return CurrentPrincipal.getPrivilege("AppFunctions.ManageContact"+ContactType); 
			}
		}

		protected string ContactType
		{
			get
			{
				string ct = m_procState.get("contactType") as string;
				if (ct == null)
					ct = "Customer";
				return ct;
			}
		}

		protected IBizObj ContactTypeBizObj
		{
			get
			{
				IBizObj contactType = App.appData[new StringBuilder(20).Append("contactOptions.types.(name:").Append(ContactType).Append(")").ToString()] as IBizObj;
				return contactType;
			}
		}

		protected override IBizObj createNewBizObj()
		{
			IBizObj newContact = App.bizObjMgr.create("Contact");
			newContact.put("id", new Swaf.BGuid.Guid());
			newContact.put("defaultAddress.id", new Swaf.BGuid.Guid());
			newContact.put("typeId", ContactTypeBizObj.get("id"));
			return newContact;
		}
		protected bool requiredFieldMissing(string[] fields, IBizObj obj)
		{
			for (int i = 0; i < fields.Length; i++)
				if (Cmn.IsEmpty(obj.get(fields[i])))
				{
					m_procState.BPC.Messenger.add("Error-RequiredFieldMissing", null);
					return true;
				}
			return false;
		}
	}
}
